package vip.xiaonuo.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.facedoor.modular.memberuser.param.MemberUserOrgChangeEventBean;
import vip.xiaonuo.facedoor.modular.topic.AiBoxEventTopic;
import vip.xiaonuo.modular.facegrouporgrelate.entity.FacegroupOrgRelate;
import vip.xiaonuo.modular.facegrouporgrelate.param.FacegroupOrgRelateParam;
import vip.xiaonuo.modular.facegrouporgrelate.service.FacegroupOrgRelateService;
import vip.xiaonuo.modular.grouporgrelatechangetemp.entity.GroupOrgRelateChangeTemp;
import vip.xiaonuo.modular.grouporgrelatechangetemp.service.GroupOrgRelateChangeTempService;
import vip.xiaonuo.modular.grouporgrelatechangetemp.service.RelateChangeTaskRunner;
import vip.xiaonuo.modular.megviifacegroup.entity.MegviiFaceGroup;
import vip.xiaonuo.modular.megviifacegroup.mapper.MegviiFaceGroupMapper;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

import static reactor.event.selector.Selectors.$;

@Service
@Slf4j
public class MemberUserOrgChangeListener implements Consumer<Event<MemberUserOrgChangeEventBean>>, InitializingBean {

    @Autowired
    private Reactor reactor;

    @Resource
    private MegviiFaceGroupMapper faceGroupMapper;

    @Resource
    private GroupOrgRelateChangeTempService tempService;

    @Resource
    private RelateChangeTaskRunner task;

    @Override
    public void afterPropertiesSet() throws Exception {
        reactor.on($(AiBoxEventTopic.ORG_EDIT), this);
    }

    @Override
    public void accept(Event<MemberUserOrgChangeEventBean> memberUserEvent) {
        try{
            MemberUserOrgChangeEventBean bean = memberUserEvent.getData();
            // 根据机构获取人脸分组
            List<MegviiFaceGroup> outFaceGroupList = faceGroupMapper.listFaceGroupByOrgId(bean.getOriginalOrgId());
            List<MegviiFaceGroup> inFaceGroupList = faceGroupMapper.listFaceGroupByOrgId(bean.getCurrentOrgId());
            List<GroupOrgRelateChangeTemp> temps = new ArrayList<>();

            for(MegviiFaceGroup group : outFaceGroupList){
                GroupOrgRelateChangeTemp temp = new GroupOrgRelateChangeTemp();
                temp.setUserId(bean.getUserId());
                temp.setFaceGroupId(group.getId());
                temp.setStatus(0);
                temp.setBizType(1);
                temp.setTimes(0);
                temps.add(temp);
            }

            for(MegviiFaceGroup group : inFaceGroupList){
                GroupOrgRelateChangeTemp temp = new GroupOrgRelateChangeTemp();
                temp.setUserId(bean.getUserId());
                temp.setFaceGroupId(group.getId());
                temp.setStatus(0);
                temp.setBizType(0);
                temp.setTimes(0);
                temps.add(temp);
            }
            // 保存中间表数据
            tempService.saveBatch(temps);
        }finally{
            new Thread(()->{
                try {
                    task.executeXxlJob();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
